package net.lab1024.sa.admin.module.upstream.product;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import net.lab1024.sa.admin.constant.AdminSwaggerTagConst;
import net.lab1024.sa.admin.module.system.login.domain.RequestEmployee;
import net.lab1024.sa.admin.module.upstream.product.domain.form.ProductAddForm;
import net.lab1024.sa.admin.module.upstream.product.domain.form.ProductQueryForm;
import net.lab1024.sa.admin.module.upstream.product.domain.ProductVO;
import net.lab1024.sa.admin.module.upstream.product.domain.form.ProductUpdateForm;
import net.lab1024.sa.admin.util.AdminRequestUtil;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Tag(name = AdminSwaggerTagConst.Upstream.UPSTREAM_PRODUCT)
public class ProductController {

    private final ProductService productService;

    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    @Operation(summary = "分页查询")
    @PostMapping("/product/page/query")
    @SaCheckPermission("product:query")
    public ResponseDTO<PageResult<ProductVO>> queryByPage(@RequestBody ProductQueryForm queryForm) {
        RequestEmployee requestUser = AdminRequestUtil.getRequestUser();
        PageResult<ProductVO> pageResult = productService.queryByPage(queryForm, requestUser);
        return ResponseDTO.ok(pageResult);
    }

    @Operation(summary = "新增产品")
    @PostMapping("/product/add")
    public ResponseDTO<String> add(@RequestBody ProductAddForm addForm) {
        return productService.add(addForm);
    }

    @Operation(summary = "修改产品")
    @PostMapping("/product/update")
    public ResponseDTO<String> update(@RequestBody ProductUpdateForm updateForm) {
        return productService.update(updateForm);
    }
}
